﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace AlarmClock.BLL
{
    public class PPImageAnimator
    {
        private static Image _preImg;//为了能执行析构回收，暂存上一次的图片句柄
        private static Image _curImg;//当前被使用的GIF图片
        private static Control _preCtrl;
        private static Control _curCtrl;

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="image">播放GIF</param>
        /// <param name="ctrl">播放GIF的控件</param>
        public static void Init(Image image, Control ctrl)
        {
            if (image != null && ctrl != null)
            {
                _preImg = _curImg;
                _preCtrl = _curCtrl;

                _curImg = image;
                _curCtrl = ctrl;

                _curCtrl.Paint += _ctrl_Paint;
            }
        }

        /// <summary>
        /// 绘制GIF
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void _ctrl_Paint(object sender, PaintEventArgs e)
        {
            if (_curImg != null && _curCtrl != null)
            {
                switch (_curCtrl)
                {
                    case PictureBox _pic:
                        e.Graphics.DrawImage(_curImg, 0, 0, _pic.Width, _pic.Height);     //从指定的位置绘制图片
                        ImageAnimator.UpdateFrames();                   //使该帧在当前正被图画处理的所有图像中前移
                        break;
                    default:
                        e.Graphics.DrawImage(_curImg, 0, 0, _curCtrl.Width, _curCtrl.Height);     //从指定的位置绘制图片
                        ImageAnimator.UpdateFrames();                   //使该帧在当前正被图画处理的所有图像中前移
                        //int x = (_curCtrl.Size.Width - 300) / 2 + (_curCtrl.Size.Width - 300) % 2;
                        //int y = (_curCtrl.Size.Height - x - 300) / 2 + (_curCtrl.Size.Height - x - 300) % 2;
                        //e.Graphics.DrawImage(_curImg, x, y, 300, 300);     //从指定的位置绘制图片
                        //ImageAnimator.UpdateFrames();                   //使该帧在当前正被图画处理的所有图像中前移
                        break;
                }

            }
        }

        /// <summary>
        /// 暂停播放
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        public static void Pause()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 开始播放
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        public static void Play()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 播放GIF【闹铃】动画
        /// </summary>
        public static void PlayRepeat()
        {
            ImageAnimator.Animate(_curImg, (s, e) =>
            {
                _curCtrl.Invalidate();
            });   //将多帧图片显示为动画图像
        }

        /// <summary>
        /// 重新播放
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        public static void RePlay()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 设置播放速度
        /// </summary>
        /// <param name="volume"></param>
        /// <exception cref="NotImplementedException"></exception>
        public static void SetSpeed(float volume)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 暂停播放
        /// </summary>
        public static void Stop()
        {
            _preImg?.Dispose();
            _preImg = null;
            ImageAnimator.StopAnimate(_curImg, (s, e) => { _curCtrl.Invalidate(); });
        }
    }
}
